﻿package Code {
	import flash.display.MovieClip;
	
	public class Base extends MovieClip {
		
		public var center:Vertex;				//the center of the top of the Base-from which the spring goes
		public var springLength:Number;        //length of the spring all together
		public var spring:Number = 0.05;        //spring constant
		public var friction:Number = 0.96;       //friction. velocity multiplied by this each time
		
		//constructor
		public function Base(center:Vertex, barVertex:Vertex) {
			this.center=center;
			init(barVertex);            //barVertex is the center of the bar so we know how 
		}
		
		//initialize
		public function init(v:Vertex)
		{
			this.x= center.x;
			this.y= center.y;
			springLength = Math.sqrt(Math.pow((center.x-v.x), 2) + Math.pow((center.y-v.y), 2));

		}
		
		//spring calculations for the bar
		public function springTo(v:Vertex)
		{			
			var dx:Number = center.x - v.x;
			var dy:Number = center.y - v.y;
			var angle:Number = Math.atan2(dy, dx);
			
			
			var targetX:Number = center.x - Math.cos(angle) * springLength;
			var targetY:Number = center.y - Math.sin(angle) * springLength;
			
			//move the vertex(the bar's b) to the appropriate place & apply friction
			v.vx += (targetX - v.x) * spring;
			v.vy += (targetY - v.y) * spring;
			v.vx *= friction;
			v.vy *= friction;
			v.x += v.vx;
			v.y += v.vy;
		}
	}
	
}
